﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using Microsoft.Phone.Shell;

namespace FinalCountdownServices.Models
{
    public class EventModel : PropertyChangedBase
    {
        private int? _id;
        public int? Id
        {
            get { return _id; }
            set
            {
                if (_id == value) return;
                _id = value;
                NotifyOfPropertyChange(() => Id);
            }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title == value) return;
                _title = value;
                NotifyOfPropertyChange(() => Title);
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description == value) return;
                _description = value;
                NotifyOfPropertyChange(() => Description);
            }
        }

        private DateTime _occurs;
        public DateTime Occurs
        {
            get { return _occurs; }
            set
            {
                if (_occurs == value) return;
                _occurs = value;
                NotifyOfPropertyChange(() => Occurs);
            }
        }

        public bool TileEnabled
        {
            get
            {
                var tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("EventId=" + Id));
                return tile != null;
            }
            set { return; }
        }

        public string TileEnabledStr
        {
            get { return TileEnabled.ToString(); }
            set { return; }
        }

        public Uri BackgroundSource { get; set; }

        //public ImageBrush BackgroundImage { get; set; }

        private bool _notificationEnabled;
        public bool NotificationEnabled
        {
            get { return _notificationEnabled; }
            set
            {
                if (_notificationEnabled == value) return;
                _notificationEnabled = value;
                NotifyOfPropertyChange(() => NotificationEnabled);
            }
        }

        private int _notificationMinutes;
        public int NotificationMinutes
        {
            get { return _notificationMinutes; }
            set
            {
                if (_notificationMinutes == value) return;
                _notificationMinutes = value;
                NotifyOfPropertyChange(() => NotificationMinutes);
            }
        }

        public int RemainingDays
        {
            get { return (Occurs - DateTime.Now).Days; }
        }

        public int RemainingHours
        {
            get { return (Occurs - DateTime.Now).Hours; }
        }

        public int RemainingMinutes
        {
            get { return (Occurs - DateTime.Now).Minutes; }
        }

        public int RemainingSeconds
        {
            get { return (Occurs - DateTime.Now).Seconds; }
        }

        public bool Obsolete
        {
            get { return Occurs < DateTime.Now; }
        }

        public string DescriptionTeaser
        {
            get
            {
                if (Description != null)
                {
                    if (Description.Length > 35)
                        return Description.Substring(0, 35) + "...";
                }
                return Description;
            }
        }
    }
}
